import PaletteProvider from "bpmn-js/lib/features/palette/PaletteProvider";
import { type PaletteEntry, type PaletteEntries } from "diagram-js/lib/features/palette/PaletteProvider";
import translate from "diagram-js/lib/i18n/translate/translate";
import { assign } from "min-dash";
import { getDi } from 'bpmn-js/lib/util/ModelUtil';

export default class CustomPalette extends PaletteProvider {

  createAction(type: string, group: any, className: any, title: any, options?: { isExpanded: any; }) :PaletteEntry{
    // @ts-expect-error
    const create = this._create;
    // @ts-expect-error
    const elementFactory = this._elementFactory;
    function createListener(event: any) {
      const shape = elementFactory.createShape(assign({ type }, options));

      if (options) {
        const di = getDi(shape);
        di.isExpanded = options.isExpanded;
      }

      create.start(event, shape);
    }

    const shortType = type.replace(/^bpmn:/, '');

    return {
      group,
      className,
      title: title || translate('Create {type}', { type: shortType }),
      action: {
        // @ts-expect-error
        dragstart: createListener,
        click: createListener
      }
    };
  }

  getPaletteEntries(): PaletteEntries {
    const result = super.getPaletteEntries();
    result['create.task'] = this.createAction(
      "bpmn:UserTask",
      "activity",
      "bpmn-icon-user-task",
      "创建用户任务"
    );

    // console.log(result);

    delete result['create.data-object'];
    delete result['create.data-store'];
    delete result['create.intermediate-event']; // 隐藏中间事件
    delete result['create.participant-expanded']; // 隐藏泳道
    return result;
  }
}